﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Resources;
using System.Windows.Threading;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace IndustrialSmokeCalc
{
    public partial class MeasurementResult : PhoneApplicationPage
    {
        private DispatcherTimer timer;
        private int result, count;

        public MeasurementResult()
        {
            InitializeComponent();

            //Uri uri = new Uri("Image\\RSC100.jpg", UriKind.Relative);
            //StreamResourceInfo resourceInfo = Application.GetResourceStream(uri);
            //BitmapImage bmp = new BitmapImage();
            //bmp.SetSource(resourceInfo.Stream);
            //result = OpacityMeasurement.Measure(bmp);

            //OpacityMeasurement.GetOpacity(DataSingleton.Instance.Picture);
            result = GetResult(DataSingleton.Instance.MeasurementResult, DataSingleton.Instance.MinuteMeasurement, DataSingleton.Instance.SecondMeasurement);
            //result = DataSingleton.Instance.RingelmannOpacity;
            //img.Source = DataSingleton.Instance.Pic;
            if (result != 0)
            {
                timer = new DispatcherTimer();
                timer.Tick += TimerTick;
                timer.Interval = TimeSpan.FromSeconds(0.0125);
                timer.Start();
            }
            else
            {
                //txtRingelmann.Text = DataSingleton.Instance.RingelmannOpacity.ToString();
                txtHasil.Text = GetCategory(result);
            }
        }

        private int GetResult(int[,] opacity, int minute, int second)
        {
            int[] level = new int[6];
            DataSingleton.Instance.StringOpacity = "";

            for (int i = 0; i < opacity.GetLength(0); i++)
            {
                for (int j = 0; j < opacity.GetLength(1); j++)
                {

                    level[opacity[i,j]]++;
                    DataSingleton.Instance.StringOpacity += opacity[i,j];
                }
                DataSingleton.Instance.StringOpacity += "|";
            }
            DataSingleton.Instance.StringOpacity = DataSingleton.Instance.StringOpacity.Remove(DataSingleton.Instance.StringOpacity.Length - 1);

            int sum = 0;
            for (int i = 1; i <= 5; i++)
            {
                sum += level[i] * i;
            }

            return sum * 20 / (minute * second);
        }

        private void TimerTick(object sender, EventArgs e)
        {
            count++;
            Gauge.Value += 1;
            if (count == result)
            {
                timer.Stop();
                //txtRingelmann.Text = DataSingleton.Instance.RingelmannOpacity.ToString();
                txtHasil.Text = GetCategory(result);
                //MessageBox.Show(DataSingleton.Instance.StringOpacity);
            }
        }

        //private void SetGauge()
        //{
        //    Gauge.Value = result;
        //    txtHasil.Text = GetCategory(result);
        //}

        private string GetCategory(int value)
        {
            return value > 40 ? "Tidak Aman" : "Aman";
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/ChooseCompany.xaml", UriKind.Relative));
        }
    }
}